package com.rabbitmq.sendmessagelate.controller;

import com.rabbitmq.sendmessagelate.config.RabbitMqConfig;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RabbitMqController {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @PostMapping("send")
    public void sendMessages(String text){
        Message message =
                MessageBuilder
                        .withBody(text.getBytes())
                        .setHeader("x-delay", 3000)
                        .build();
        //设置下交换机和队列
        rabbitTemplate.convertAndSend(RabbitMqConfig.EXCHANGE_NAME,RabbitMqConfig.QUEUE_NAME,message);
    }
}
